package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.OrderItem;
import com.ericom.service.GenericManager;

public class OrderItemAction extends BaseAction {
	private GenericManager<OrderItem, Long> orderItemManager;
	private List orderItems;
	private OrderItem orderItem;
	private Long id;

	public OrderItem getOrderItem() {
		return orderItem;
	}

	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setOrderItemManager(
			GenericManager<OrderItem, Long> orderItemManager) {
		this.orderItemManager = orderItemManager;
	}

	public String list() {
		orderItems = orderItemManager.getAll();
		return SUCCESS;
	}

	public List getOrderItems() {
		return orderItems;
	}

	public String delete() {
		orderItemManager.remove(orderItem.getId());
		this.saveMessage(getText("orderItem.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			orderItem = orderItemManager.get(id);
		} else {
			orderItem = new OrderItem();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (orderItem.getId() == null);
		orderItem = orderItemManager.save(orderItem);

		String key = (isNew) ? "orderItem.added" : "orderItem.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
